<?php

namespace app\common\model;

use app\common\model\BaseModel;
use think\Exception;


class Vip extends BaseModel
{
    // 表名
    protected $name = 'vip';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];
    

    
    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'trash' => __('Trash')];
    }


    public function getEscortOvertimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['escort_overtime']) ? $data['escort_overtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }



    public static function onBeforeUpdate($data)
    {
        if ($data['status']=='normal') {
            $check_exist=self::where([['id','<>',$data['id']],['level','=',$data['level']],['status','=','normal']])->find();
            if($check_exist){
                throw new Exception('等级与其他启用中的会员信息重复。');
            }
        }
    }

    public static function onBeforeInsert($data)
    {
        if ($data['status']=='normal') {
            $check_exist=self::where([['level','=',$data['level']],['status','=','normal']])->find();
            if($check_exist){
                throw new Exception('等级与其他启用中的会员信息重复。');
            }
        }
    }


}
